/**
 * Created by lixuegang on 2016/1/4.
 */
// 页面加载初始化方法
$(function () {
    initDataGrid();
});

// 数据表格初始化方法
function initDataGrid() {
    $('#displayTable').datagrid({
        nowrap: true, //是否换行
        autoRowHeight: false, //自动行高
        fitColumns: true,
        fit: true,
        striped: true,
        collapsible: true, //是否可折叠
        sortName: 'createTime',
        sortOrder: 'asc',
        remoteSort: true,
        idField: 'wordId',
        singleSelect: false, //是否单选
        pagination: true, //分页控件
        scrollbarSize: 0,
        loadMsg: "数据加载中.....",
        rowStyler: function (index, row) {
            if (row.state == 3 || row.state == 4) {
                return "color:red;";
            }
        },
        frozenColumns: [
            [
                {field: 'ck', checkbox: true},
                {
                    field: 'wordId', title: '<span class="columnTitle">序号</span>', width: 40, sortable: true,
                    formatter: function (value, row, index) {
                        return '<a href="#" style="color:blue; text-decoration:none;" ' + 'onclick="doDisplay(\'' + row.wordId + '\');">' + row.wordId + '</a>';
                    }
                }
            ]
        ],
        columns: [
            [
                {field: 'wordTitle', title: '<span class="columnTitle">标题</span>', width: 100},
                {
                    field: 'stateName', title: '<span class="columnTitle">状态</span>', width: 40
                },
                {
                    field: 'keyWord', title: '<span class="columnTitle">关键词</span>', width: 100
                },
                {
                    field: 'summary', title: '<span class="columnTitle">摘要</span>', width: 100
                },
                {
                    field: 'deliveryTime', title: '<span class="columnTitle">发布时间</span>', width: 60, sortable: true,
                    formatter: function (value) {
                        if (value == null) {
                            return value;
                        } else {
                            return formatDate(value);
                        }
                    }
                },
                {
                    field: 'createTime', title: '<span class="columnTitle">创建时间</span>', width: 60, sortable: true,
                    formatter: function (value) {
                        if (value == null) {
                            return value;
                        } else {
                            return formatDate(value);
                        }
                    }
                },
                {
                    field: 'updateTime', title: '<span class="columnTitle">更新时间</span>', width: 60, sortable: true,
                    formatter: function (value) {
                        if (value == null) {
                            return value;
                        } else {
                            return formatDate(value);
                        }
                    }
                },
                {
                    field: 'showDetail', title: '<span class="columnTitle">详情</span>', width: 40,
                    formatter: function (value, row) {
                        return '<a href="#" style="color:red;text-decoration:none" onclick="showDetail(\'' + row.wordId + '\');">详细</a>';
                    }
                },
                {
                    field: 'updateType', title: '<span class="columnTitle">操作</span>', width: 40,
                    formatter: function (value, row) {
                        return '<a href="#" style="color:red;text-decoration:none" onclick="doUpdate(\'' + row.wordId + '\');">修改</a>';
                    }
                }
            ]
        ],
        toolbar: "#dataGridToolbar",
        onLoadSuccess: function () {
            $('#displayTable').datagrid('clearSelections');
        },
        onLoadError: function (httpRequest) {
            displayErrorMessage(httpRequest);
        },
        pageSize: 20, //每页显示的记录条数，默认为10
        pageList: [20, 40, 60, 80, 100], //可以设置每页记录条数的列表
        beforePageText: '第', //页数文本框前显示的汉字
        afterPageText: '页    共 {pages} 页',
        displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录'
    });
}

// 请求信息查询方法
function doSearch() {
    var queryParams = $('#displayTable').datagrid('options').queryParams;
    queryParams.wordId = $('#search_wordId').val();
    queryParams.wordTitle = $('#search_wordTitle').val();
    //queryParams.wordType = $('#search_wordType').val();
    queryParams.keyword = $('#search_keyword').val();
    queryParams.state = $('#video_state').combobox('getValue');
    queryParams.delTag = $('#delTag').combobox('getValue');
    queryParams.queryType = $('#dateOption').combobox('getValue');
    queryParams.startDate = $("input[name='startDate']").val();
    queryParams.endDate = $("input[name='endDate']").val();
    $('#displayTable').datagrid({url: 'queryLikeWord.html'});
}


// 添加信息
function doInsert() {
    var title = '请求添加';
    var url = 'insertLikeTemplate.html';
    initWindow(title, url, 850, 600);
}

// 提交插入表单
function insertSubmit() {
    var url = 'insertLikeWord.html';
    $('#submitForm').form('submit', {
        url: url,
        success: function (response) {
            displayMessage(response);
        }
    });
}
//修改卡信息
function doUpdate(value) {
    var title = '更新信息';
    var url = 'forwardUpdateWord.html?wordId=' + value;
    initWindow(title, url, 850, 600);
}

// 提交更新表单
function updateSubmit() {
    var url = 'updateLikeWord.html';
    $('#updateForm').form('submit', {
        url: url,
        success: function (response) {
            displayMessage(response);
        }
    });
}

// 提交更新表单
function releaseWord() {
    var url = 'releaseWord.html';
    $('#updateForm').form('submit', {
        url: url,
        success: function (response) {
            displayMessage(response);
        }
    });
}

//删除方法
function deleteSubmit() {
    var ids = [];
    var rows = $('#displayTable').datagrid('getSelections');
    if (rows.length == 0) {
        promptMessage("请至少选择一个删除信息！");
    } else {
        $.messager.confirm('删除信息', '您确定要删除选中的信息吗?', function (r) {
            if (r) {
                for (var i = 0; i < rows.length; i++) {
                    ids.push(rows[i].wordId);
                }
                $("#wordId").val(ids.join(':'));
                $('#deleteForm').form('submit', {
                    url: 'deleteLikeWord.html',
                    success: function (response) {
                        displayMessage(response);
                    }
                });
            }
        })
    }
}
function doDisplay(value) {
    var title = '查看信息';
    var url = 'displayWord.html?wordId=' + value;
    initWindow(title, url, 850, 600);
}
function showDetail(value) {
    var title = '详细信息';
    var url = 'showDetailWord.html?wordId=' + value;
    initWindow(title, url, 850, 600);
}